﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;


namespace BusinessLogic
{
    public static class DiemThuongKhachHang
    {

        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.DiemThuongKhachHang[] Select()
        {
            Entities.DiemThuongKhachHang[] list = null;
            try
            {
                string sql = Common.Constants.DiemThuongKhachHang.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.DiemThuongKhachHang DiemThuongKhachHang = new Entities.DiemThuongKhachHang();
                    DiemThuongKhachHang.DiemDaDung = dr[Common.Constants.DiemThuongKhachHang.DiemDaDung].ToString();
                    DiemThuongKhachHang.DiemThuongKhachHangID = int.Parse(dr[Common.Constants.DiemThuongKhachHang.DiemThuongKhachHangID].ToString());
                    DiemThuongKhachHang.MaDiemThuongKhachHang = dr[Common.Constants.DiemThuongKhachHang.MaDiemThuongKhachHang].ToString();
                    DiemThuongKhachHang.MaKhachHang = dr[Common.Constants.DiemThuongKhachHang.MaKhachHang].ToString();
                    DiemThuongKhachHang.TenKhachHang = dr[Common.Constants.DiemThuongKhachHang.TenKhachHang].ToString();
                    DiemThuongKhachHang.TongDiem = dr[Common.Constants.DiemThuongKhachHang.TongDiem].ToString();
                    DiemThuongKhachHang.DiemConLai = (int.Parse(DiemThuongKhachHang.TongDiem) - int.Parse(DiemThuongKhachHang.DiemDaDung)).ToString();
                    DiemThuongKhachHang.GhiChu = dr[Common.Constants.DiemThuongKhachHang.GhiChu].ToString();
                    DiemThuongKhachHang.Deleted = bool.Parse(dr[Common.Constants.DiemThuongKhachHang.Deleted].ToString());
                    arr.Add(DiemThuongKhachHang);
                }
                list = (Entities.DiemThuongKhachHang[])arr.ToArray(typeof(Entities.DiemThuongKhachHang));
                return list;
            }
            catch
            {
                return new Entities.DiemThuongKhachHang[0];
            }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region Insert update
        public static int InsertUpdate(Entities.DiemThuongKhachHang DiemThuongKhachHang)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.DiemThuongKhachHang.InsertUpdate;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.HanhDong, SqlDbType.NVarChar, 50).Value = DiemThuongKhachHang.HanhDong;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.DiemThuongKhachHangID, SqlDbType.Int).Value = DiemThuongKhachHang.DiemThuongKhachHangID;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.MaDiemThuongKhachHang, SqlDbType.VarChar, 50).Value = DiemThuongKhachHang.MaDiemThuongKhachHang;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.MaKhachHang, SqlDbType.VarChar, 50).Value = DiemThuongKhachHang.MaKhachHang;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.TenKhachHang, SqlDbType.NVarChar, 200).Value = DiemThuongKhachHang.TenKhachHang;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.TongDiem, SqlDbType.Int).Value = DiemThuongKhachHang.TongDiem;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.DiemDaDung, SqlDbType.Int).Value = DiemThuongKhachHang.DiemDaDung;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.GhiChu, SqlDbType.NVarChar, 200).Value = DiemThuongKhachHang.GhiChu;
                cmd.Parameters.Add(Common.Constants.DiemThuongKhachHang.Deleted, SqlDbType.Bit).Value = DiemThuongKhachHang.Deleted;
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                return rt;
            }
            catch
            {
                return 0;
            }

        }
        #endregion
    }
}
#endregion